package com.softserve.fproject.cashier;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;
import javax.persistence.TypedQuery;
import java.util.*;
import com.softserve.fproject.domain.*;

@RequestMapping("/authorization/**")
@Controller
public class Authorization {

    /*@RequestMapping
    public void get(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    }

    @RequestMapping(method = RequestMethod.POST, value = "{id}")
    public void post(@PathVariable Long id, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    }
*/
    @RequestMapping
    public String index(Model uiModel) {
		//uiModel.addAttribute("fligth", "fligthFromI");
		//uiModel.addAttribute("fligthlist", Flight.findAllFlights());
        return "authorization/index";
    }
	
	@RequestMapping(value="/current")
    public String currentFlights(Model uiModel) {
		uiModel.addAttribute("flightlist", Flight.findAllFlights());
		
		//System.out.println("NAAAAAAAASTYAAAAA");
		//System.out.println(Flight.findAllFlights());		
        return "authorization/current";
    }
	
	@RequestMapping(value="/booking")
    public String booking(Model uiModel) {
		uiModel.addAttribute("countries", Country.findAllCountrys());
		uiModel.addAttribute("cities", City.findAllCitys());		
        return "authorization/booking";
    }
	
	@RequestMapping(value="/getCountries")
    public String findCitiesByCountry(Model uiModel) {
		uiModel.addAttribute("countries", Country.findAllCountrys());
		uiModel.addAttribute("cities", City.findAllCitys());		
        return "authorization/booking";
    }
	
	@RequestMapping(value="/booking/search")
    public String findHopsFromTo(Model uiModel, /*@RequestParam(value="from")*/Long idFrom, /*@RequestParam(value="to")*/Long idTo , Date onDate) {		
		//System.out.println(idFrom);
		//System.out.println(idTo);
		idFrom=1l;
		idTo=2l;
		onDate=new Date(System.currentTimeMillis()-2*24*60*60*1000);				
		Date onDate2=new Date(System.currentTimeMillis()+2*24*60*60*1000);
		List<Airport> froms=(Airport.findAirportsByCity(City.findCity(idFrom))).getResultList();		
		List<Airport> tos=Airport.findAirportsByCity(City.findCity(idTo)).getResultList();
		//System.out.println(froms);
		//System.out.println(tos);
		List<Hop> hops=new ArrayList<Hop>();		
		for(Airport a:froms)
		{
			for(Airport b:tos)
			{
				//List<Hop> fromAtoB=Hop.findHopsByToAirportAndFromAirport(b, a).getResultList();
				//System.out.println(fromAtoB);
				hops.addAll(Hop.findHopsByToAirportAndFromAirport(b, a).getResultList());
			}
		}
		
		List<Flight> flightsByDate=Flight.findFlightsByOnDateBetween(onDate, onDate2).getResultList();
		//List<Flight> flightsByDate=Flight.findAllFlights();
		System.out.println(flightsByDate);
		List<Flight> flights=new ArrayList<Flight>();
		for(Flight f:flightsByDate)
		{
			for(Hop h:hops)
			{
				if(f.getHops().contains(h))
				{
					if(!flights.contains(f))
						flights.add(f);					
				}
			}
		}
		uiModel.addAttribute("hops", hops);				
		uiModel.addAttribute("flightlist", flights);
		System.out.println(flights);
        return "authorization/current";
    }
	
	@RequestMapping(value = "booking/flightlist/{id}", method = RequestMethod.GET)
	public String showFlight(@PathVariable("id") Long id, Model uiModel) {        
		uiModel.addAttribute("flight", Flight.findFlight(id));
        return "authorization/booking/flight";
    }
}
